package com.maila.xiaoaodispatch.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.RadioButton;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.maila.xiaoaodispatch.R;
import com.maila.xiaoaodispatch.adpter.MyOrderAdapter;
import com.maila.xiaoaodispatch.adpter.OrderHallAdapter;
import com.maila.xiaoaodispatch.api.DispatchApi;
import com.maila.xiaoaodispatch.entity.OrderData;
import com.maila.xiaoaodispatch.net.VolleyInterface;
import com.maila.xiaoaodispatch.utils.StartActivityUtils;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn_user)
    AutoRelativeLayout btnUser;
    @BindView(R.id.rb_hall)
    RadioButton rbHall;
    @BindView(R.id.rb_myorder)
    RadioButton rbMyorder;
    @BindView(R.id.rl_title)
    AutoRelativeLayout rlTitle;
    @BindView(R.id.lv_mall_order)
    ListView lvMallOrder;
    @BindView(R.id.btn_stop)
    AutoRelativeLayout btnRefresh;

    int type=0;
    OrderHallAdapter orderHallAdapter;
    MyOrderAdapter myOrderAdapter;
    int  hallpage=1;
    int mypage=1;
    int size=8;
    private String adcode="1";
    Gson gson=new Gson();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        orderHallAdapter=new OrderHallAdapter(this);
        myOrderAdapter=new MyOrderAdapter(this);
        lvMallOrder.setAdapter(orderHallAdapter);
    }

    @OnClick({R.id.btn_user, R.id.rb_hall, R.id.rb_myorder, R.id.btn_stop})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_user:
                StartActivityUtils.start(this,PersonalActivity.class);
                break;
            case R.id.rb_hall:
                type=0;
                setPage();
                break;
            case R.id.rb_myorder:
                type=1;
                setPage();
                break;
            case R.id.btn_stop:
                break;
        }
    }


   void setPage(){
       if(type==0){
        lvMallOrder.setAdapter(orderHallAdapter);
           rbHall.setChecked(true);
           rbMyorder.setChecked(false);
           btnRefresh.setVisibility(View.VISIBLE);
           lvMallOrder.setAdapter(orderHallAdapter);
           initdata();
       }else {
           lvMallOrder.setAdapter(myOrderAdapter);
           rbHall.setChecked(false);
           rbMyorder.setChecked(true);
           btnRefresh.setVisibility(View.GONE);
           lvMallOrder.setAdapter(myOrderAdapter);
       }
   }

    private void initdata() {
       if(type==1){
           new DispatchApi(this, new VolleyInterface() {
               @Override
               public void onSuccess(String result) {
                   try {
                       OrderData orderData=gson.fromJson(result,OrderData.class);
                       if(orderData.getResult()==1){

                       }else {

                       }
                   } catch (JsonSyntaxException e) {
                       e.printStackTrace();
                   }
               }

               @Override
               public void onError(VolleyError error) {

               }
           }).get_all_order_list(hallpage,size,adcode);
       }else {
            new DispatchApi(this, new VolleyInterface() {
                @Override
                public void onSuccess(String result) {

                }

                @Override
                public void onError(VolleyError error) {

                }
            }).get_unfinished_order_list(mypage,size,adcode);
       }
    }

}
